package day7;

import org.junit.Test;

import java.sql.*;
import java.util.ArrayList;

public class JDBCDemo {
    @Test
    public void test1() {
            // 1、加载驱动Class.forName("");
        try {
            Class.forName("com.mysql.jdbc.Driver");
            // 2、获得连接对象Connection
            Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/java211202?useUnicode=true&characterEncoding=UTF-8", "root", "1234");
            // 3、写sql语句
             String sql = "SELECT id,`name`,age,gender FROM student";
//            int age = 23;
//            String sql = "SELECT id,`name`,age,gender FROM student where age=" + age;
            // 4、创建Statement(一艘船)
            Statement statement = connection.createStatement();
            // 5、执行sql语句
            // (1) 更新类（更改了表里面数据）：delete/update/insert     executeUpdate()
            // 返回值：int，表示你影响的行数
            //  (2)查询（没有改变表里面数据）:  select                  executeQuery()
            // 返回值：结果集ResultSet
            ResultSet resultSet = statement.executeQuery(sql);
            ArrayList<Student> list = new ArrayList<>();
            // 判断下一个有没有，如果有返回true，并指向下一行
            while (resultSet.next()) {
                int id = resultSet.getInt("id");
                String name = resultSet.getString("name");
                int age = resultSet.getInt("age");
                String gender = resultSet.getString("gender");
                Student student = new Student(id, name, age, gender);
                list.add(student);
            }
            for (Student student : list) {
                System.out.println(student);
            }
            // 6、关闭连接
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
    }

    @Test
    public void testPreparedStatement() {
        //int age = 23;
        try {
            Connection connection = JDBCUtil.getConnection();
            String sql = "SELECT id,`name`,age,gender FROM student where age=? and name=?";
            // 预编译Statement
            PreparedStatement statement = connection.prepareStatement(sql);
            statement.setInt(1, 23);
            statement.setString(2, "张三丰");
            System.out.println(statement);
            ResultSet resultSet = statement.executeQuery();
            ArrayList<Student> list = new ArrayList<>();
            while (resultSet.next()) {
                int id = resultSet.getInt("id");
                String name = resultSet.getString("name");
                int age = resultSet.getInt("age");
                String gender = resultSet.getString("gender");
                Student student = new Student(id, name, age, gender);
                list.add(student);
            }
            for (Student student : list) {
                System.out.println(student);
            }
        }  catch (SQLException throwables) {
            throwables.printStackTrace();
        }
    }

    @Test
    public void testPreparedStatement2() {
        //int age = 23;
        Connection connection = null;
        PreparedStatement statement = null;
        ResultSet resultSet = null;
        try {
            connection = JDBCUtil.getConnection();
            String sql = "SELECT id,`name`,age,gender FROM student";
            // 预编译Statement
            statement = connection.prepareStatement(sql);
            System.out.println(statement);
            resultSet = statement.executeQuery();
            ArrayList<Student> list = new ArrayList<>();
            while (resultSet.next()) {
                int id = resultSet.getInt("id");
                String name = resultSet.getString("name");
                int age = resultSet.getInt("age");
                String gender = resultSet.getString("gender");
                Student student = new Student(id, name, age, gender);
                list.add(student);
            }
            for (Student student : list) {
                System.out.println(student);
            }
        }  catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            JDBCUtil.close(resultSet, statement, connection);
            /*if (resultSet != null) {
                try {
                    resultSet.close();
                } catch (SQLException throwables) {
                    throwables.printStackTrace();
                }
            }
            if (statement != null) {
                try {
                    statement.close();
                } catch (SQLException throwables) {
                    throwables.printStackTrace();
                }
            }
            if (connection != null) {
                try {
                    connection.close();
                } catch (SQLException throwables) {
                    throwables.printStackTrace();
                }
            }*/
        }
    }

    @Test
    public void testInsert() {
        Connection connection = null;
        PreparedStatement statement = null;
        try {
            connection = JDBCUtil.getConnection();
            String sql = "INSERT INTO student(`name`,age,gender) VALUES(?,?,?)";
            statement = connection.prepareStatement(sql);
            statement.setString(1, "小李");
            statement.setInt(2, 24);
            statement.setString(3, "男");
            System.out.println(statement);
            int count = statement.executeUpdate();
            System.out.println("count: " + count);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            JDBCUtil.close(null, statement, connection);
            /*if (statement != null) {
                try {
                    statement.close();
                } catch (SQLException throwables) {
                    throwables.printStackTrace();
                }
            }
            if (connection != null) {
                try {
                    connection.close();
                } catch (SQLException throwables) {
                    throwables.printStackTrace();
                }
            }*/
        }
    }

    @Test
    public void testDelete() {
        try {
            Connection connection = JDBCUtil.getConnection();
            String sql = "delete from student where id=?";
            PreparedStatement statement = connection.prepareStatement(sql);
            statement.setInt(1, 9);
            System.out.println(statement);
            int count = statement.executeUpdate();
            System.out.println("count: " + count);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
    }

    @Test
    public void testUpdate() {
        try {
            Connection connection = JDBCUtil.getConnection();
            String sql = "update student set name=?,age=?,gender=? where id=?";
            PreparedStatement statement = connection.prepareStatement(sql);
            statement.setString(1, "小张");
            statement.setInt(2, 23);
            statement.setString(3, "男");
            statement.setInt(4, 8);
            System.out.println(statement);
            int count = statement.executeUpdate();
            System.out.println("count: " + count);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
    }

    // 验证sql注入问题
    @Test
    public void testPreparedStatement3() {
        try {
            Connection connection = JDBCUtil.getConnection();
            String sql = "SELECT id,name,password FROM users WHERE `name`=? AND `password`=?";
            // 预编译Statement
            PreparedStatement statement = connection.prepareStatement(sql);
            statement.setString(1, "zhangsan' OR 1=1 -- y");
            statement.setString(2, "123");
            System.out.println(statement);
            ResultSet resultSet = statement.executeQuery();
            ArrayList<Student> list = new ArrayList<>();
            while (resultSet.next()) {
                int id = resultSet.getInt("id");
                String name = resultSet.getString("name");
                int age = resultSet.getInt("age");
                String gender = resultSet.getString("gender");
                Student student = new Student(id, name, age, gender);
                list.add(student);
            }
            for (Student student : list) {
                System.out.println(student);
            }
        }  catch (SQLException throwables) {
            throwables.printStackTrace();
        }
    }

    public static void main(String[] args) {

    }
}
